package com.shd.edanpei.presenter;

import android.app.Dialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ExceptionHandler;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.OrderListModel;
import com.shd.edanpei.bean.TakeOrderBean;
import com.shd.edanpei.bean.PagingModel;
import com.shd.edanpei.presenter.contract.HomeContract;
import com.shd.edanpei.enums.OrderTypeEnum;
import com.shd.edanpei.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/10/19.
 * yang-wen-wen
 */

public class HomePresenter extends BasePresenterImpl<HomeContract.HomeIView>
        implements HomeContract.HomeIPresenter {

    private int currentPage;

    public HomePresenter(HomeContract.HomeIView mView) {
        super(mView);
    }

    @Override
    public void showTakeDialog(final int orderID, final int index) {
        View view = View.inflate(mBaseIView.getContext(), R.layout.dialog_take_code, null);
        final EditText etCode = view.findViewById(R.id.et_code);
        View sure = view.findViewById(R.id.sure);
        final Dialog takeDialog = new Dialog(mBaseIView.getContext(), R.style.inputDialog);
        takeDialog.setContentView(view);
        takeDialog.setCanceledOnTouchOutside(true);
        takeDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                etCode.setText("");
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String code = etCode.getText().toString();
                if (!TextUtils.isEmpty(code)) {
                    takeDialog.dismiss();
                    takeOrder(orderID, index, code);
                }
            }
        });
        takeDialog.show();
    }

    @Override
    public void allRefresh() {
        currentPage = 1;
        ApiCreator.getApi().findAllOrdres(currentPage)
                .compose(RxTransformer.<PagingModel<OrderListModel>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<OrderListModel>>() {
                    @Override
                    protected void onSuccess(PagingModel<OrderListModel> data) {
                        if (data != null && data.getList().size() > 0) {
                            currentPage++;
                            List<OrderListModel> list = new ArrayList<>();
                            for (int i = 0; i < data.getList().size(); i++) {
                                if (data.getList().get(i).getOrderType() != OrderTypeEnum.normal.getKey()) {
                                    if (i < 6) {
                                        list.add(data.getList().get(i));
                                    }
                                }

                            }

                            if (list.size() != 0) {
                                mBaseIView.getAdapter().setNewData(list);
                                mBaseIView.addFootView();
                            } else {
                                mBaseIView.removeFootView();
                            }

                        } else {
                            mBaseIView.getAdapter().setNewData(null);
                            mBaseIView.removeFootView();
//                            mBaseIView.getAdapter().setEmptyView(LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.null_home, "暂无相关订单"));
                        }

                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        if (throwable.getCode() == ExceptionHandler.UNUPLOAD_PRODUCT) {
                            //未上架产品
//                            mBaseIView.getAdapter().setEmptyView(getUnUnploadLayout(throwable.getMessage()));
                        } else {
//                            mBaseIView.getAdapter().setEmptyView(LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.null_home, "暂无相关订单"));
                        }

                        mBaseIView.removeFootView();
                    }

                    @Override
                    public void onFinish() {
                        if (mBaseIView != null) {
                            mBaseIView.getRefreshLayout().setRefreshing(false);
                        }
                    }
                });
    }

    private void takeOrder(int orderID, final int index, final String code) {
        mBaseIView.showLoading("接单中...");
        ApiCreator.getApi().takeOrder(orderID, code)
                .compose(RxTransformer.<TakeOrderBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<TakeOrderBean>() {
                    @Override
                    protected void onSuccess(TakeOrderBean data) {
                        if (data.getCode().equals("1")) {
                            if (mBaseIView.getAdapter().getData().size() > 0) {
                                mBaseIView.getAdapter().remove(index);
                            }
                            DialogUtil.showTakeSuccessDialog(mBaseIView.getContext(), new DialogUtil.TwoListener() {

                                @Override
                                public void cancel() {

                                }

                                @Override
                                public void sure() {
                                    RxBus.get().send(RxBusCode.CHECK_SHARE_ORDER);
                                }
                            });
                        } else if (data.getCode().equals("3")) {
                            ToastUtil.showToast(data.getMsg());
                            if (mBaseIView.getAdapter().getData().size() > 0) {
                            }
                        }

                    }

                    @Override
                    public void onFinish() {
                        mBaseIView.stopLoading();
                    }
                });
    }


    @Override
    public void refresh() {
        currentPage = 1;
        ApiCreator.getApi().robOrderList(currentPage)
                .compose(RxTransformer.<PagingModel<OrderListModel>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<OrderListModel>>() {
                    @Override
                    protected void onSuccess(PagingModel<OrderListModel> data) {
                        if (data != null && data.getList().size() > 0) {
                            currentPage++;
                            List<OrderListModel> list = new ArrayList<>();

                            for (int i = 0; i < data.getList().size(); i++) {
                                if (data.getList().get(i).getOrderType() != OrderTypeEnum.normal.getKey()) {
                                    if (i < 6) {
                                        list.add(data.getList().get(i));
                                    }
                                }

                            }

                            if (list.size() != 0) {
                                mBaseIView.getAdapter().setNewData(list);
                                mBaseIView.addFootView();
                            } else {
                                mBaseIView.removeFootView();
                            }

                        } else {
                            mBaseIView.getAdapter().setNewData(null);
                            mBaseIView.removeFootView();
                        }

                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        if (throwable.getCode() == ExceptionHandler.UNUPLOAD_PRODUCT) {
                            //未上架产品
//                            mBaseIView.getAdapter().setEmptyView(getUnUnploadLayout(throwable.getMessage()));
                        } else {
//                            mBaseIView.getAdapter().setEmptyView(LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.null_home, "暂无相关订单"));
                        }
                        mBaseIView.removeFootView();
                    }

                    @Override
                    public void onFinish() {
                        if (mBaseIView != null) {
                            mBaseIView.getRefreshLayout().setRefreshing(false);
                        }
                    }
                });
    }

}
